package com.zhiwang.planet.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.RectF;

import androidx.annotation.Nullable;

import android.util.AttributeSet;
import android.view.View;

import com.zhiwang.planet.R;

//
public class InertiaProgressBar extends View {
    private static final String TAG = "InertiaProgressBar";

    private int progressBarcolor;

    //持续进度的时间
//    private int intervalTime;

    //进度条宽度
    private int barThickness;

    //进度条半径
    private int barRadius;

    private int progressbarPadding;

    private float progress = 0f;

    private boolean continuing = true;

    private Paint paint;

    private Paint bgPaint;

    public int period;//周期，多少秒转一圈

    private Runnable ContinuingTask = new Runnable() {

        @Override
        public void run() {
            continuing = false;
            invalidate();
        }
    };
    private RectF rectf;
    private Paint layerPaint;
    private int barLayerColor;


    public InertiaProgressBar(Context context) {
        this(context, null);
    }

    public InertiaProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InertiaProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        loadAttr(context, attrs);
        initPaint();
    }

    private void loadAttr(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InertiaProgressBar);
        progressBarcolor = typedArray.getColor(R.styleable.InertiaProgressBar_ip_progress_bar_color, Color.parseColor("#6945CD"));
        barLayerColor = typedArray.getColor(R.styleable.InertiaProgressBar_ip_progress_bar_layer_color, Color.parseColor("#F1F8FF"));
        barRadius = typedArray.getDimensionPixelSize(R.styleable.InertiaProgressBar_ip_progress_bar_radius, 55);
        barThickness = typedArray.getDimensionPixelSize(R.styleable.InertiaProgressBar_ip_progress_bar_thickness, 12);
        progressbarPadding = typedArray.getDimensionPixelSize(R.styleable.InertiaProgressBar_ip_progress_bar_padding, 30);
        period = typedArray.getInteger(R.styleable.InertiaProgressBar_ip_period, 180);
//        counter = ic_generate_new Counter(Long.MAX_VALUE, 100);
        //180000
        typedArray.recycle();
    }

    private void initPaint() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(barThickness);
        paint.setColor(progressBarcolor);
        paint.setStrokeCap(Cap.ROUND);
        paint.setStyle(Style.STROKE);

        layerPaint = new Paint(paint);
        layerPaint.setColor(barLayerColor);

        bgPaint = new Paint(paint);
        bgPaint.setColor(Color.WHITE);
        bgPaint.setStyle(Style.FILL);
        bgPaint.setStrokeWidth(0.1f);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int hw = barRadius * 2 + barThickness * 2 + progressbarPadding * 2;
        setMeasuredDimension(hw, hw);
        if (rectf == null) {
            int delta = barThickness / 2 + progressbarPadding;
            rectf = new RectF(delta, delta, hw - delta, hw - delta);
        }
    }


    int time = 0;

    @Override
    protected void onDraw(Canvas canvas) {
//        if (continuing) {
//            if (time + 50f >= (period * 1000)) {
//                progress = 0f;
//                time = 0;
//                pause();
//                onCompleteListener.onComplete();
//            } else {
//                time += 50f;
//                progress = (time + 50f) / (period * 1000f);
//            }
//        }
        float angle = 360 * progress;
        canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredWidth() / 2, getMeasuredWidth() / 2, bgPaint);
        canvas.drawArc(rectf, 0, 360, false, layerPaint);
        canvas.drawArc(rectf, -90, angle, false, paint);
//        if (continuing)
//            postInvalidateDelayed(50);
    }


    public void setProgress(float progress) {
        this.progress = progress;
        postInvalidate();
    }

    public void pause() {
        continuing = false;
        postInvalidate();
    }

    public void cancel() {
        if (!continuing) return;
//        counter.cancel();
        continuing = false;
        postInvalidate();
    }

    public void start() {
        if (continuing) return;
        continuing = true;
        postInvalidate();
    }

    public void restore() {
        if (!continuing) {
            continuing = true;
            time = 0;
            postInvalidate();
        }
    }

    private OnCompleteListener onCompleteListener;

    public void setOnCompleteListener(OnCompleteListener onCompleteListener) {
        this.onCompleteListener = onCompleteListener;
    }

    //进度百分百发放奖励
    public interface OnCompleteListener {
        void onComplete();
    }

}
